<?php 
session_start();
header('Content-Type: text/html; charset=utf-8');
include_once("./c/config.php");
$access_token = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appId.'&secret='.$secret.'&code='.$_GET['code'].'&grant_type=authorization_code';
$access = file_get_contents($access_token);
$accessObj = json_decode($access);
$access_token = $accessObj->access_token;
$openid   = $accessObj->openid;
$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
$userInfoObj = json_decode(file_get_contents($user_info_url));
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}
if ($mysqli->connect_errno) {
	printf("Connect failed: %s\n", $mysqli->connect_error);
	exit;
}
$sql="select id from users where openid='".$openid."'  limit 0,1 ";
$query = $mysqli->query($sql);
if($query->num_rows>0){
}else{
	if (isset($openid) && !empty($openid)){
		$sql = "insert into users(openid,nickname,headimgurl,createtime) values ('".$openid."','".addslashes(trim($userInfoObj->nickname))."', '".$userInfoObj->headimgurl."', '".date("Y-m-d H:i:s")."' )";
		$mysqli->query($sql);
	}
}
$_SESSION['openid']=$openid;
header('Location: game.php?op='.$openid);
exit;